package com.zy.test.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.test.entity.Course;
import com.zy.test.entity.Schedule;
import com.zy.test.service.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 排课表：
 * 上课周期，上课起始时间，上课结束时间，科目ID，教师ID ，教室ID，班级ID，备注，新增时间， (Schedule)表控制层
 *
 * @author jhr
 * @since 2023-04-03 16:32:30
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ScheduleService scheduleService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private CourseService courseService;
    @Resource
    private RoomService roomService;
    @Resource
    private ClasseService classeService;

    /**
     * 分页查询所有数据
     *
     * @param schedule 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectByPaging")
    public R selectAll(Schedule schedule) {
        Page<Schedule> page = new Page<>(schedule.getPageNum(), schedule.getPageSize());
        LambdaQueryWrapper<Schedule> scheduleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        scheduleLambdaQueryWrapper.ge(!ObjectUtils.isEmpty(schedule.getBegTime()), Schedule::getBegTime, schedule.getBegTime())
                .le(!ObjectUtils.isEmpty(schedule.getEndTime()), Schedule::getEndTime, schedule.getEndTime())
                .eq(!ObjectUtils.isEmpty(schedule.getCoCode()), Schedule::getCoCode, schedule.getCoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getRoCode()), Schedule::getRoCode, schedule.getRoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getClCode()), Schedule::getClCode, schedule.getClCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeCode()), Schedule::getTeCode, schedule.getTeCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeller()), Schedule::getTeller, schedule.getTeller())
                .eq(!ObjectUtils.isEmpty(schedule.getMemo()), Schedule::getMemo, schedule.getMemo());
//        if (!ObjectUtils.isEmpty(schedule.getBegTime())) {
//            scheduleLambdaQueryWrapper.ge(Schedule::getBegTime, schedule.getBegTime());
//        }
//        if (!ObjectUtils.isEmpty(schedule.getEndTime())) {
//            scheduleLambdaQueryWrapper.le(Schedule::getEndTime, schedule.getEndTime());
//        }
//        if (!ObjectUtils.isEmpty(schedule.getClCode())) {
//            scheduleLambdaQueryWrapper.eq(Schedule::getClCode,schedule.getClCode()).eq();
//        }
//        if (!ObjectUtils.isEmpty(schedule.getCoCode())) {
//            scheduleLambdaQueryWrapper.eq(Schedule::getCoCode,schedule.getCoCode());
//        }
//        if (!ObjectUtils.isEmpty(schedule.getRoCode())) {
//            scheduleLambdaQueryWrapper.eq(Schedule::getRoCode,schedule.getRoCode());
//        }
        Page<Schedule> page1 = this.scheduleService.page(page, scheduleLambdaQueryWrapper);
        if (page1.getRecords().size() > 0) {
            for (Schedule record : page1.getRecords()) {
                // 科目名称
                if (!ObjectUtils.isEmpty(record.getCoCode())){
                    record.setCoName(courseService.getById(record.getCoCode()).getCoName());
                }
                // 班级名称
                if (!ObjectUtils.isEmpty(record.getClCode())){
                    record.setClName(classeService.getById(record.getClCode()).getClName());
                }
                // 教师名称
                if (!ObjectUtils.isEmpty(record.getTeCode())){
                    record.setTeName(teacherService.getById(record.getTeCode()).getTeName());
                }
                // 教室
                if (!ObjectUtils.isEmpty(record.getRoCode())){
                    record.setRoName(roomService.getById(record.getRoCode()).getRoName());
                }
            }
        }
        return success(page1);
    }

    /**
     * 展示一周数据
     * @param schedule
     * @return
     */
    @GetMapping("/selectByWeek")
    public R selectByWeek(Schedule schedule) {
        Page<Schedule> page = new Page<>(schedule.getPageNum(), schedule.getPageSize());
        LambdaQueryWrapper<Schedule> scheduleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        scheduleLambdaQueryWrapper.ge(!ObjectUtils.isEmpty(schedule.getBegTime()), Schedule::getBegTime, schedule.getBegTime())
                .le(!ObjectUtils.isEmpty(schedule.getEndTime()), Schedule::getEndTime, schedule.getEndTime())
                .eq(!ObjectUtils.isEmpty(schedule.getCoCode()), Schedule::getCoCode, schedule.getCoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getRoCode()), Schedule::getRoCode, schedule.getRoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getClCode()), Schedule::getClCode, schedule.getClCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeCode()), Schedule::getTeCode, schedule.getTeCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeller()), Schedule::getTeller, schedule.getTeller())
                .eq(!ObjectUtils.isEmpty(schedule.getMemo()), Schedule::getMemo, schedule.getMemo());

        Page<Schedule> page1 = this.scheduleService.page(page, scheduleLambdaQueryWrapper);
        if (page1.getRecords().size() > 0) {
            for (Schedule record : page1.getRecords()) {
                // 科目名称
                if (!ObjectUtils.isEmpty(record.getCoCode())){
                    record.setCoName(courseService.getById(record.getCoCode()).getCoName());
                }
                // 班级名称
                if (!ObjectUtils.isEmpty(record.getClCode())){
                    record.setClName(classeService.getById(record.getClCode()).getClName());
                }
                // 教师名称
                if (!ObjectUtils.isEmpty(record.getTeCode())){
                    record.setTeName(teacherService.getById(record.getTeCode()).getTeName());
                }
                // 教室
                if (!ObjectUtils.isEmpty(record.getRoCode())){
                    record.setRoName(roomService.getById(record.getRoCode()).getRoName());
                }
            }
        }
        return success(page1);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.scheduleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param schedule 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public R insert(@RequestBody Schedule schedule) {
        schedule.setCreDate(DateUtil.date());
        return success(this.scheduleService.save(schedule));
    }

    /**
     * 修改数据
     *
     * @param schedule 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public R update(@RequestBody Schedule schedule) {

        // 条件构造器：暂时理解 where 条件拼接逻辑
        LambdaUpdateWrapper<Schedule> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.eq(!ObjectUtils.isEmpty(schedule.getCoCode()),Schedule::getCoCode, schedule.getCoCode());
        wrapper.set(!ObjectUtils.isEmpty(schedule.getCoCode()),Schedule::getCoCode, schedule.getCoCode());// 等价于：set name = yy// 等价于：where id = 20

//        wrapper.eq(!ObjectUtils.isEmpty(schedule.getTeCode()),Schedule::getTeCode, schedule.getTeCode());      // 等价于：where id = 20
        wrapper.set(!ObjectUtils.isEmpty(schedule.getTeCode()),Schedule::getTeCode, schedule.getTeCode());

        wrapper.eq(!ObjectUtils.isEmpty(schedule.getScheduleId()),Schedule::getScheduleId, schedule.getScheduleId());
        wrapper.set(!ObjectUtils.isEmpty(schedule.getRoCode()),Schedule::getRoCode, schedule.getRoCode());
        wrapper.set(!ObjectUtils.isEmpty(schedule.getMemo()),Schedule::getMemo, schedule.getMemo());
        wrapper.set(Schedule::getModiDate, DateUtil.date());

        scheduleService.update(null, wrapper);
        return success(this.scheduleService.updateById(schedule));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Schedule schedule) {
        LambdaQueryWrapper<Schedule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(schedule.getDate()), Schedule::getDate, schedule.getDate())
                .ge(!ObjectUtils.isEmpty(schedule.getBegTime()), Schedule::getBegTime, schedule.getBegTime())
                .le(!ObjectUtils.isEmpty(schedule.getEndTime()), Schedule::getEndTime, schedule.getEndTime())
                .eq(!ObjectUtils.isEmpty(schedule.getCoCode()), Schedule::getCoCode, schedule.getCoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getRoCode()), Schedule::getRoCode, schedule.getRoCode())
                .eq(!ObjectUtils.isEmpty(schedule.getClCode()), Schedule::getClCode, schedule.getClCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeCode()), Schedule::getTeCode, schedule.getTeCode())
                .eq(!ObjectUtils.isEmpty(schedule.getTeller()), Schedule::getTeller, schedule.getTeller());

        return success(this.scheduleService.remove(lambdaQueryWrapper));
    }
}

